import * as roleService from '../services/system/RoleService';
import * as userService from '../services/system/UserService';
import { judgePermission,isloadPermission,setPermission } from '../common/shiro';
import { Constants } from '../common/constant/constants';
import {stringListToInt} from '../common/utils/stringUtils';

export default {
  namespace: 'role',

  state: {
    data: {
      list: [],
      pagination: {},
    },
    loading: false, 
    permission:{},    
    allRoleList:[],
    // menuIdList:[],
  },

  effects: {
      *judgePermission(_, { call, put }){
          if(!isloadPermission()){
              const response = yield call(userService.getPermissions);
              setPermission(response.result)
          }
          var permission = judgePermission(Constants.SYSTEM_ROLE.PERM);
          yield put({
              type: 'savePermission',
              payload:permission,
          });
          return permission;
      },
    *fetch({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(roleService.queryRole, payload);
      const returnData = {
        list:response.result.list,
        pagination:{
          pageSize:response.result.pageSize,
          total:response.result.total,
          pageIndex:payload.pageIndex,
        },
      }
      yield put({
        type: 'save',
        payload: returnData,
      });
    },
    *fetchAllRole({ payload }, { call, put }) {
      const response = yield call(roleService.queryAllRole);
      // yield put({
      //   type: 'saveAllRole',
      //   payload: response.result,
      // });
      return response.result;
    },

    *fetchRoleMenuIds({ payload }, { call, put }){
      const response = yield call(roleService.queryRoleMenu, payload);
      return response.result.menuIdList;
    },
    *post({ payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      let response;
      payload.menuIdList = stringListToInt(payload.menuIdList);
      if(payload.roleId==0){//新建
        response = yield call(roleService.saveRole,payload);
      }else{//修改
        response = yield call(roleService.updateRole,payload);
      }
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      return response;
    },
    *delete({ payload }, { call, put }){
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(roleService.deleteRole,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      return response;
    },
  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        data: action.payload,
        loading:false,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    // saveAllRole(state, { payload }){
    //   return {
    //     ...state,
    //     allRoleList:payload,
    //   }
    // },
    savePermission(state,{payload}){
      return {
        ...state,
        permission:payload,
      }
    },
  },
};
